﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace YTSK.Common
{
    /// <summary>
    /// 注册表处理类
    /// </summary>
    class HandleRegistry
    {

        #region 在HKEY_LOCAL_MACHINE\SOFTWARE下新建名为cKey的注册表项。如果已经存在则不影响
        /// <summary>
        ///在HKEY_LOCAL_MACHINE\SOFTWARE下新建名为cKey的注册表项。如果已经存在则不影响！
        /// </summary>
        /// <param name="cKey"></param>
        /// <returns></returns>
        public static void createClass(string cKeyClass)
        {
            if (isClassExist(cKeyClass))
            { return; }
            RegistryKey key = Registry.CurrentUser;
            RegistryKey software = key.CreateSubKey("software\\" + cKeyClass);
        }
        #endregion 在HKEY_LOCAL_MACHINE\SOFTWARE下新建名为cKey的注册表项。如果已经存在则不影响

        #region 打开注册表的键
        /// <summary>
        /// 打开注册表的键
        /// </summary>
        /// <param name="cKeyClass"></param>
        /// <returns></returns>
        public static RegistryKey openClass(string cKeyClass)
        {
            createClass(cKeyClass);
            RegistryKey key = Registry.CurrentUser;
            RegistryKey software = key.OpenSubKey("software\\" + cKeyClass, true);
            return software;
            //注意该方法后面还可以有一个布尔型的参数，true表示可以写入。
        }
        #endregion

        #region 保存注册表健
        /// <summary>
        /// 保存注册表健
        /// </summary>
        /// <param name="sKey">健</param>
        /// <param name="sKeyValue">健的值</param>
        /// <param name="sKeyClass">分类</param>
        public static void saveKey(string sKey, string sKeyValue, string sKeyClass)
        {
            RegistryKey software = openClass(sKeyClass); //该项必须已存在
            string oldValue = readKey(sKey, sKeyClass);
            if (oldValue == sKeyValue)
            { return; }
            else
            {
                software.SetValue(sKey, sKeyValue);
            }
            //在HKEY_LOCAL_MACHINE\SOFTWARE\test下创建一个名为“test”，值为“博客园”的键值。如果该键值原本已经存在，则会修改替换原来的键值，如果不存在则是创建该键值。
            // 注意：SetValue()还有第三个参数，主要是用于设置键值的类型，如：字符串，二进制，Dword等等~~默认是字符串。如：
            // software.SetValue("test", "0", RegistryValueKind.DWord); //二进制信息
            software.Close();
        }
        #endregion

        #region 获取注册表某个键的值
        /// <summary>
        /// 获取注册表某个键的值
        /// </summary>
        /// <param name="sKey">健</param>
        /// <param name="sKeyClass">分类</param>
        /// <returns></returns>
        public static string readKey(string sKey, string sKeyClass)
        {
            string info = "";
            if (!isClassExist(sKeyClass)) { return ""; }
            if (!isKeyExit(sKey, sKeyClass)) { return ""; }
            RegistryKey myreg = openClass(sKeyClass);
            info = myreg.GetValue(sKey).ToString();
            myreg.Close();
            return info;
        }
        #endregion 获取注册表某个键的值

        #region 删掉某个注册表的值
        /// <summary>
        /// 删掉某个注册表的值
        /// </summary>
        /// <param name="sKey">键</param>
        /// <param name="sKeyClass">所属分类</param>
        public static void deleteKey(string sKey, string sKeyClass)
        {
            if (!isClassExist(sKeyClass)) { return; }
            if (!isKeyExit(sKey, sKeyClass)) { return; }
            RegistryKey delkey = openClass(sKeyClass);
            delkey.DeleteValue(sKey);
            delkey.Close();
        }
        #endregion 删掉某个注册表的值

        #region 删掉某个类别
        /// <summary>
        /// 删掉某个类别
        /// </summary>
        /// <param name="cKeyClass">需要删除的分类</param>
        public static void deleteClass(string cKeyClass)
        {
            if (!isClassExist(cKeyClass)) { return; }
            RegistryKey key = Registry.CurrentUser;
            key.DeleteSubKey("software\\" + cKeyClass, true); //该方法无返回值，直接调用即可
            key.Close();
        }
        #endregion


        #region 判断注册表项是否存在
        /// <summary>
        /// 判断注册表项是否存在
        /// </summary>
        /// <returns></returns>
        private static bool isClassExist(string keyClass)
        {
            string[] subkeyNames;
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE");
            //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);  
            subkeyNames = software.GetSubKeyNames();
            //取得该项下所有子项的名称的序列，并传递给预定的数组中  
            foreach (string keyName in subkeyNames)
            //遍历整个数组  
            {
                if (keyName == keyClass)
                //判断子项的名称  
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
        #endregion

        #region 判断键值是否存在
        /// <summary>
        /// 判断键值是否存在
        /// </summary>
        /// <returns></returns>
        private static bool isKeyExit(string cKey, string cKeyClass)
        {
            if (!isClassExist(cKeyClass)) { return false; }
            string[] subkeyNames;
            RegistryKey software = openClass(cKeyClass);
            //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
            subkeyNames = software.GetValueNames();
            //取得该项下所有键值的名称的序列，并传递给预定的数组中
            foreach (string keyName in subkeyNames)
            {
                if (keyName == cKey) //判断键值的名称
                {
                    software.Close();
                    return true;
                }

            }
            software.Close();
            return false;
        }
        #endregion 判断键值是否存在
    }
}
